package boot

import (
	"github.com/gin-gonic/gin"
	"io"
	"log"
	"os"
	"server/acgnovel-go/router"
	"server/acgnovel-go/src/config"
)

func GinBoot() {
	r := gin.Default()
	//日志 记录到文件
	err := os.MkdirAll("./log", os.ModePerm)
	if err != nil {
		log.Println(err)
	}
	logFile, e := os.OpenFile(`log/gin-request.log`, os.O_WRONLY|os.O_CREATE|os.O_APPEND, os.ModePerm)
	if e != nil {
		log.Println("Gin 日志始化失败！")
		config.ErrorConfig <- e
	}
	gin.DefaultWriter = io.MultiWriter(logFile)

	//自定义日志
	//gin.DebugPrintRouteFunc = func(httpMethod, absolutePath, handlerName string, nuHandlers int) {
	//	log.Printf("endpoint %v %v %v %v\n", httpMethod, absolutePath, handlerName, nuHandlers)
	//}

	//授权过滤
	r.Use(router.AuthFilter())

	//init router
	initRouter(r)

	go func() {
		err := r.Run()
		if err != nil {
			log.Println("Gin始化失败！")
			config.ErrorConfig <- err
		}
	}()
}

func initRouter(r *gin.Engine) {
	router.WebRouter(r)
	router.StaticRouter(r)
	router.AccessRouter(r)
	router.UserRouter(r)
}
